package com.base.commons.tools.page;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 *
 * @author carl
 * @since 1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "分页数据")
public class PageData<T> implements Serializable {
    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "总记录数")
    private long total;

    @ApiModelProperty(value = "列表数据")
    private List<T> list;

    @JsonProperty("prev_token")
    @ApiModelProperty("es 返回的页面值, 请求上一页时用此值拼 page_token 的值")
    private String prevToken;

    @JsonProperty("next_token")
    @ApiModelProperty("es 返回的页面值, 请求下一页时用此值拼 page_token 的值")
    private String nextToken;


    /**
     * 分页
     * @param list   列表数据
     * @param total  总记录数
     */
    public PageData(List<T> list, long total) {
        this.list = list;
        this.total = total;
    }

    public static <T> PageData<T> emptyReturn() {
        return new PageData<>(Collections.emptyList(), 0L);
    }
}
